const StringAlgorithms = require("../template")

function testStringMatching() {
  const stringAlgo = new StringAlgorithms()

  console.log(
    "1. KMP字符串匹配算法测试:",
    (() => {
      const tests = [
        { text: "hello world", pattern: "world", expected: 6 },
        { text: "ababcabab", pattern: "abab", expected: 0 },
        { text: "hello", pattern: "ll", expected: 2 },
        { text: "abc", pattern: "d", expected: -1 },
        { text: "", pattern: "a", expected: -1 },
        { text: "a", pattern: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.kmpSearch(test.text, test.pattern) === test.expected)
    })()
  )

  console.log(
    "2. Boyer-Moore字符串匹配算法测试:",
    (() => {
      const tests = [
        { text: "hello world", pattern: "world", expected: 6 },
        { text: "ababcabab", pattern: "abab", expected: 0 },
        { text: "hello", pattern: "ll", expected: 2 },
        { text: "abc", pattern: "d", expected: -1 },
        { text: "", pattern: "a", expected: -1 },
        { text: "a", pattern: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.boyerMooreSearch(test.text, test.pattern) === test.expected)
    })()
  )

  console.log(
    "3. Rabin-Karp字符串匹配算法测试:",
    (() => {
      const tests = [
        { text: "hello world", pattern: "world", expected: 6 },
        { text: "ababcabab", pattern: "abab", expected: 0 },
        { text: "hello", pattern: "ll", expected: 2 },
        { text: "abc", pattern: "d", expected: -1 },
        { text: "", pattern: "a", expected: -1 },
        { text: "a", pattern: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.rabinKarpSearch(test.text, test.pattern) === test.expected)
    })()
  )

  console.log(
    "4. 字符串匹配算法一致性测试:",
    (() => {
      const text = "ababcabab"
      const pattern = "abab"
      const kmpResult = stringAlgo.kmpSearch(text, pattern)
      const boyerMooreResult = stringAlgo.boyerMooreSearch(text, pattern)
      const rabinKarpResult = stringAlgo.rabinKarpSearch(text, pattern)
      return kmpResult === boyerMooreResult && boyerMooreResult === rabinKarpResult
    })()
  )

  console.log(
    "5. 长文本匹配性能测试:",
    (() => {
      const longText = "a".repeat(1000) + "b".repeat(1000) + "c".repeat(1000)
      const pattern = "b" + "c".repeat(100)
      const start = performance.now()
      const result = stringAlgo.kmpSearch(longText, pattern)
      const end = performance.now()
      return result !== -1 && (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "6. this上下文字符串匹配测试:",
    (() => {
      const matchingObj = {
        multiplier: 2,
        searchString: function(text, pattern) {
          const stringAlgo = new StringAlgorithms()
          const result = stringAlgo.kmpSearch(text, pattern)
          return result * this.multiplier
        }
      }
      return matchingObj.searchString("hello", "ll") === 4 // 2 * 2 = 4
    })()
  )

  console.log(
    "7. 复杂对象this绑定字符串匹配测试:",
    (() => {
      const matchingConfigObj = {
        config: { offset: 5 },
        searchWithConfig: function(text, pattern) {
          const stringAlgo = new StringAlgorithms()
          const result = stringAlgo.boyerMooreSearch(text, pattern)
          return result === -1 ? -1 : result + this.config.offset
        }
      }
      return matchingConfigObj.searchWithConfig("hello", "ll") === 7 // 2 + 5 = 7
    })()
  )

  console.log(
    "8. 字符串匹配边界测试:",
    (() => {
      const emptyTextResult = stringAlgo.kmpSearch("", "a") === -1 &&
                             stringAlgo.boyerMooreSearch("", "a") === -1 &&
                             stringAlgo.rabinKarpSearch("", "a") === -1
      
      const emptyPatternResult = stringAlgo.kmpSearch("hello", "") === 0 &&
                                 stringAlgo.boyerMooreSearch("hello", "") === 0 &&
                                 stringAlgo.rabinKarpSearch("hello", "") === 0
      
      return emptyTextResult && emptyPatternResult
    })()
  )

  console.log(
    "9. 字符串匹配特殊字符测试:",
    (() => {
      const specialText = "hello world! 123 @#$"
      const specialPattern = "world!"
      const kmpResult = stringAlgo.kmpSearch(specialText, specialPattern)
      const boyerMooreResult = stringAlgo.boyerMooreSearch(specialText, specialPattern)
      const rabinKarpResult = stringAlgo.rabinKarpSearch(specialText, specialPattern)
      return kmpResult === 6 && boyerMooreResult === 6 && rabinKarpResult === 6
    })()
  )

  console.log(
    "10. 字符串匹配重复模式测试:",
    (() => {
      const text = "aaaaaa"
      const pattern = "aa"
      const kmpResult = stringAlgo.kmpSearch(text, pattern)
      const boyerMooreResult = stringAlgo.boyerMooreSearch(text, pattern)
      const rabinKarpResult = stringAlgo.rabinKarpSearch(text, pattern)
      return kmpResult === 0 && boyerMooreResult === 0 && rabinKarpResult === 0
    })()
  )
}

testStringMatching()
